//
//  NewPersonalVc.m
//  Shengzhuli
//
//  Created by FXGuo on 2018/1/17.
//  Copyright © 2018年 kamangkeji. All rights reserved.
//

#import "NewPersonalVc.h"
#import "NewPersonalInfoCell.h"
#import "NewPersonalSingleCell.h"
#import "PersonTableViewCell.h"
#import "SZLAccountHelper.h"
#import "PersonNetWork.h"
#import "MBProgressHUD+MJ.h"
#import "SZLTabBarViewController.h"
#import "SZLPersonInfoViewController.h"
#import "NewBecomeVipVc.h"
#import "PersonInfo.h"
#import "NewCardVc.h"
#import "ConIntegralController.h"
#import "SZLMykeepViewController.h"
#import "SZLAccountViewController.h"
#import "TicketVc.h"
#import "ZhujiaoListController.h"
#import "SZLReceiveAddressViewController.h"
#import "ConpickUpViewController.h"
#import "ConMyTeamController.h"
#import "ConClubShowViewController.h"
#import "ConMyClubEditViewController.h"
#import "ConGoodsManagerController.h"
#import "SZLMessageListViewController.h"
#import "ConPeronalSetViewController.h"
#import "BuyCarsViewController.h"
#import "ConNewEditCardVc.h"
#import "SZLSingleListViewController.h"
#import "ReadyActivityVc.h"
#import "ConNewClubDetailVc.h"

@interface NewPersonalVc ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView *tableView;
@property (nonatomic,strong) NSArray *titles;
@property (nonatomic,strong) PersonInfo *info;

@end

@implementation NewPersonalVc

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"";
//    self.titles = @[@"助教管理",@"收货地址",@"捡漏专区",@"约咖管理",@"已报名活动"];
    self.titles = @[@"助教管理",@"收货地址",@"捡漏专区",@"已报名活动"];
    [self.view addSubview:self.tableView];
    [self setNavi];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self getPersonInfo];
    });
}
- (void)setNavi
{
    UIButton *setBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [setBtn setImage:GetImage(@"设置_new") forState:UIControlStateNormal];
    setBtn.tag = 0;
    [setBtn addTarget:self action:@selector(actionClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *setItem = [[UIBarButtonItem alloc] initWithCustomView:setBtn];
    
    UIButton *messageBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [messageBtn setImage:GetImage(@"消息_new") forState:UIControlStateNormal];
    messageBtn.tag = 1;
    [messageBtn addTarget:self action:@selector(actionClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *messageItem = [[UIBarButtonItem alloc] initWithCustomView:messageBtn];
    self.navigationItem.leftBarButtonItems = @[setItem,messageItem];
    
    UIButton *buyCarBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    [buyCarBtn setImage:GetImage(@"购物车_new") forState:UIControlStateNormal];
    buyCarBtn.tag = 2;
    [buyCarBtn addTarget:self action:@selector(actionClicked:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *buyCarItem = [[UIBarButtonItem alloc] initWithCustomView:buyCarBtn];
    self.navigationItem.rightBarButtonItem = buyCarItem;
}
- (void)actionClicked:(UIButton *)btn
{
    if (btn.tag == 0) {
        ConPeronalSetViewController *setVc = [[ConPeronalSetViewController alloc] init];
        setVc.allowStutas = self.info.allowStutas;
        [self.navigationController pushViewController:setVc animated:YES];
    }else if (btn.tag == 1) {
        SZLMessageListViewController *messageVc = [[SZLMessageListViewController alloc]init];
        [self .navigationController pushViewController:messageVc animated:YES];
    }else {
        BuyCarsViewController *carVc = [[BuyCarsViewController alloc]init];
        [self.navigationController pushViewController:carVc animated:YES];
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2) {
        return 30;
    }
    return 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 30)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 12;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 180;
    }else if (indexPath.section == 1) {
        if (self.info.roleId == 4) {
            return 115;
        }
        return 230;
    }
    return 50;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 2) {
        return self.titles.count;
    }
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        NewPersonalInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NewPersonalInfoCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.info = self.info;
        cell.typeClickBlock = ^(NSInteger tag) {
            [self jumpTovcWithTag:tag];
        };
        return cell;
    }else if (indexPath.section == 1){
        NewPersonalSingleCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([NewPersonalSingleCell class])];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.typeActionBlock = ^(NSInteger tag) {
            [self singleVipClick:tag];
        };
        cell.clipsToBounds = YES;
        return cell;
    }
    PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([PersonTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.title = self.titles[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        switch (indexPath.row) {
            case 0:
                {
                    ZhujiaoListController *vc = [[ZhujiaoListController alloc]init];
                    [self.navigationController pushViewController:vc animated:YES];
                }
                break;
            case 1:
            {
                SZLReceiveAddressViewController *vc = [[SZLReceiveAddressViewController alloc]init];
                vc.type = AddressTypeNone;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 2:
            {
                ConpickUpViewController *vc = [[ConpickUpViewController alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 3:
            {
                ReadyActivityVc *vc = [[ReadyActivityVc alloc]init];
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
//            case 3:
//            {
//                [MBProgressHUD showOnlyText:@"施工中，稍后开放" view:self.view];
//            }
//                break;
//            case 4:
//            {
//                ReadyActivityVc *vc = [[ReadyActivityVc alloc]init];
//                [self.navigationController pushViewController:vc animated:YES];
//            }
//                break;
            default:
                break;
        }
    }
}
//section1点击
- (void)singleVipClick:(NSInteger)tag
{
    switch (tag) {
        case -1:
        {
            SZLSingleListViewController *listVc = [[SZLSingleListViewController alloc]init];
            [self.navigationController pushViewController:listVc animated:YES];
        }
            break;
        case 0:case 1:case 2:case 3:
        {
            SZLSingleListViewController *listVc = [[SZLSingleListViewController alloc]init];
            listVc.type = (SingleType)(tag+1);
            [self.navigationController pushViewController:listVc animated:YES];
        }
            break;
        case 4:
        {
            ConMyTeamController *teamVc = [[ConMyTeamController alloc]init];
            [self.navigationController pushViewController:teamVc animated:YES];
        }
            break;
        case 5:
        {
            ConNewClubDetailVc *vc = [[ConNewClubDetailVc alloc]init];
            vc.ID = [self.info.clubId integerValue];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 6:
        {
            ConGoodsManagerController *mgrVc = [[ConGoodsManagerController alloc]init];
            [self.navigationController pushViewController:mgrVc animated:YES];
        }
            break;
        case 7:
        {
            [MBProgressHUD showOnlyText:@"施工中,稍后开放" view:self.view];
        }
            break;
        default:
            break;
    }
}

//section0点击
- (void)jumpTovcWithTag:(NSInteger)tag
{
    switch (tag) {
        case 0:
            {
                ConNewEditCardVc *editVc = [[ConNewEditCardVc alloc]init];
                editVc.info = self.info;
                [self.navigationController pushViewController:editVc animated:YES];
            }
            break;
        case 1:
        {
            NewBecomeVipVc *vipVc = [[NewBecomeVipVc alloc]init];
            vipVc.info = self.info;
            [self.navigationController pushViewController:vipVc animated:YES];
        }
            break;
        case 2:
        {
            NewCardVc *cardVc = [[NewCardVc alloc]init];
            cardVc.info = self.info;
            [self.navigationController pushViewController:cardVc animated:YES];
        }
            break;
        case 3:
        {
            TicketVc *vc = [[TicketVc alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
            break;
        case 4:
        {
            SZLMykeepViewController *keepVc = [[SZLMykeepViewController alloc]init];
            keepVc.type = MyListKeep;
            [self.navigationController pushViewController:keepVc animated:YES];
        }
            break;
        case 5:
        {
            ConIntegralController *conVc = [[ConIntegralController alloc]init];
            [self.navigationController pushViewController:conVc animated:YES];
        }
            break;
        case 6:
        {
            SZLAccountViewController *acountVc = [[SZLAccountViewController alloc]init];
            [self.navigationController pushViewController:acountVc animated:YES];
        }
            break;
        default:
            break;
    }
}
//个人信息请求
- (void)getPersonInfo
{
    NSMutableDictionary *parame = [NSMutableDictionary dictionary];
    parame[@"token"] = [SZLAccountHelper shareInstance].token;
//    [self showClearBgWaiter];
    [PersonNetWork getPersonInfoWithParame:parame response:^(BOOL success, NSString *msg,NSString *errorCode,PersonInfo *info) {
//        [self hideWaiter];
        [self hideReloadView];
        if (success) {
            //支付后需要在这更新roleID权限
            [SZLAccountHelper shareInstance].roleId = info.roleId;
            [SZLAccountHelper shareInstance].name = info.name;
            self.info = info;
//            NewPersonalInfoCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
//            cell.info = info;
            [self.tableView reloadData];
        }else{
//            if ([msg isEqualToString:NetworkError]) {
//                [self showReloadViewWithTarget:self action:@selector(getPersonInfo)];
//            }
            [MBProgressHUD showError:msg toView:self.view];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if ([errorCode isEqualToString:NoLoginStatus]) {
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    SZLTabBarViewController *barVc = (SZLTabBarViewController*)[UIApplication sharedApplication].keyWindow.rootViewController;
                    barVc.selectedIndex = 0;
                    [SZLAccountHelper clearAlreadyInfo];
                    [SZLAccountHelper shouldLogin];
                }
            });
        }
    }];
}

- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-SafeAreaTopHeight-SafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = RGBColor(238, 238, 244);
        _tableView.tableFooterView = [[UIView alloc]init];
        _tableView.showsVerticalScrollIndicator = NO;
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewPersonalInfoCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NewPersonalInfoCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([NewPersonalSingleCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([NewPersonalSingleCell class])];
        [_tableView registerNib:[UINib nibWithNibName:NSStringFromClass([PersonTableViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([PersonTableViewCell class])];
        
    }
    return _tableView;
}

@end
